# coding: utf-8
from pymongo import MongoClient


class MongoConn(object):

    def __init__(self, host='localhost', port=27017, db='nlp'):
        self.client = MongoClient(host, port)
        self.db = self.client[db]

    def insert_one(self, json, collection='data'):
        result = self.db[collection].insert_one(json)
        return result

    def is_in(self, json, collection='data'):
        # 如果要用_id，必须转成ObjectID（from bson.objectid import ObjectId）
        if self.db[collection].find_one(json):
            return True
        else:
            return False

    def find(self, json, collection='data'):
        result = []
        for item in self.db[collection].find(json):
            result.append(item)
        return result

    def find_all(self, collection='data'):
        result = []
        for item in self.db[collection].find({}):
            result.append(item)
        return result
